#include<bits/stdc++.h>
using namespace std;
#define pc putchar
#define pb push_back
#define mp make_pair
#define ls (id<<1)
#define rs ((id<<1)|1)
#define mid ((l+r)>>1)
#define int long long
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rof(i,a,b) for(int i=(a);i>=(b);--i)
char buf[(1<<21)+5],*p1,*p2;
#define gc() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)

int read(){
    int ret=0,f=0; char ch=gc();
    while(ch<'0'||ch>'9') f|=(ch=='-'),ch=gc();
    while(ch>='0'&&ch<='9') ret=(ret<<1)+(ret<<3)+(ch^48),ch=gc();
    return f?-ret:ret;
}

void write(int x){
    if(!x) return pc('0'),void();
    if(x<0) pc('-'),x=-x;
    int stk[30],tp=0;
    while(x) stk[++tp]=x%10,x/=10;
    while(tp) pc('0'+stk[tp--]);
    return;
}

const int Maxn=410;
int N,A[Maxn],hed[Maxn],cnt,Ans;
struct node{int nxt,to;}G[Maxn<<1];
bool Vis[Maxn];

void Addedge(int x,int y){G[++cnt]=(node){hed[x],y};hed[x]=cnt;}

void DFS(int x,int ret){
    if(x>N) return Ans=max(Ans,ret),void();
    For(i,1,N)if(!Vis[i]){
        Vis[i]=1;
        for(int j=hed[i];j;j=G[j].nxt)
            A[G[j].to]+=A[i];
        DFS(x+1,ret+A[i]);
        for(int j=hed[i];j;j=G[j].nxt)
            A[G[j].to]-=A[i];
        Vis[i]=0;
    }
}

signed main(){
    freopen("light.in","r",stdin);
    freopen("light.out","w",stdout);
    N=read(); For(i,1,N) A[i]=read();
    For(i,1,N-1){
        int x=read(),y=read();
        Addedge(x,y),Addedge(y,x);
    }
    DFS(1,0); write(Ans);
    return 0;
}